package it.tukano.treditor.modules;

import it.tukano.app.AppModule;
import it.tukano.check.Todo;
import it.tukano.fun.VFunction1;
import it.tukano.reflection.MethodExt;
import it.tukano.treditor.engine.EngineQueryInterface;
import it.tukano.treditor.engine.geom.NTuple2;
import it.tukano.treditor.engine.scene.SceneElement;
import it.tukano.treditor.modules.datamodel.DataModelEvent;
import it.tukano.treditor.modules.datamodel.DataModelListener;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;

/**
 * Engine tool that picks things
 * @author pgi
 */
public class PickTool extends BasicModule implements DataModelListener, MouseListener {
    private EngineManager engineManager;
    private EngineQueryInterface engineQueryInterface;
    private DataModel dataModel;

    /**
     * Instance initializer
     */
    public PickTool() {
    }

    @Override
    public void startModule(AppModule context) {
        super.startModule(context);
        invokeWithModules("installToolButton", ToolbarModule.class);
    }
    
    public void installToolButton(ToolbarModule bar) {
        Icon icon = getIcon("pick", 24);
        bar.createToggleButton(0, icon, new MethodExt(this, "activate"), new MethodExt(this, "deactivate"));
    }
    
    public void activate() {
        invokeWithModules("installTool", EngineManager.class, DataModel.class);
    }
    
    public void deactivate() {
        invokeWithModules("uninstallTool", EngineManager.class, DataModel.class);
    }
    
    public void installTool(EngineManager em, DataModel dataModel) {
        Component engineDisplay = em.getEngineDisplay();
        engineDisplay.addMouseListener(this);
        this.engineManager = em;
        this.engineQueryInterface = em.getEngineQueryInterface();
        this.dataModel = dataModel;
    }
    
    public void uninstallTool(EngineManager em, DataModel dataModel) {
        Component engineDisplay = em.getEngineDisplay();
        engineDisplay.removeMouseListener(this);
    }

    @Override
    public void dataModelEventPerformed(DataModelEvent e) {
        if(e.getEventSource() == this) return;
        
        switch(e.getType()) {
            default: Todo.implementThis();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        final NTuple2 screenPoint = new NTuple2(e.getX(), e.getY());
        final SceneElement sceneRoot = dataModel.getSceneRoot();
        engineQueryInterface.pickElementAtScreenPoint(screenPoint, sceneRoot, setPickedObject);
    }
    
    private final VFunction1<SceneElement> setPickedObject = new VFunction1<SceneElement>() {

        @Override
        protected void doApply(SceneElement p) {
            dataModel.setSelectedElement(PickTool.this, p);
        }
    };

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
}
